/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*

   Copyright 2009 BBC Research, David Lewis, <david.lewis4@bbc.co.uk>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package MessageHandler;

import java.awt.Color;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;


public class Admin extends javax.swing.JFrame {

    /** Creates new form Manager */
    public Admin() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelDB = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldHost = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldPort = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldDB = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFielduser = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabelURI = new javax.swing.JLabel();
        jButtonConnect = new javax.swing.JButton();
        jPanelEvent = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabelEventID = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextFieldEventName = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButtonCreate = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        jComboBoxExisting = new javax.swing.JComboBox();
        jButtonUse = new javax.swing.JButton();
        jPanelMonitor = new javax.swing.JPanel();
        jButtonStart = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListMonitor = new javax.swing.JList();
        jButtonExit = new javax.swing.JButton();
        jButtonPort = new javax.swing.JButton();
        jPanelMonitor1 = new javax.swing.JPanel();
        jButtonStart1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListMonitor1 = new javax.swing.JList();
        jButtonExit1 = new javax.swing.JButton();
        jButtonPort1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Host:");

        jTextFieldHost.setText("localhost");
        jTextFieldHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldHostActionPerformed(evt);
            }
        });

        jLabel2.setText("Port:");

        jTextFieldPort.setText("3306");
        jTextFieldPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPortActionPerformed(evt);
            }
        });

        jLabel3.setText("Database:");

        jTextFieldDB.setText("Events");
        jTextFieldDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldDBActionPerformed(evt);
            }
        });

        jLabel4.setText("Username:");

        jTextFielduser.setText("root");
        jTextFielduser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFielduserActionPerformed(evt);
            }
        });

        jLabel5.setText("Password:");

        jPasswordFieldPassword.setText("test");
        jPasswordFieldPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordFieldPasswordActionPerformed(evt);
            }
        });

        jLabel6.setText("URI:");

        jLabelURI.setText(" ");

        jButtonConnect.setText("Connect");
        jButtonConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConnectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelDBLayout = new javax.swing.GroupLayout(jPanelDB);
        jPanelDB.setLayout(jPanelDBLayout);
        jPanelDBLayout.setHorizontalGroup(
            jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDBLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDBLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelDBLayout.createSequentialGroup()
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelDBLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldHost, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelDBLayout.createSequentialGroup()
                                .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelURI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextFielduser, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelDBLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldDB, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                            .addComponent(jButtonConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6))
                .addContainerGap())
        );
        jPanelDBLayout.setVerticalGroup(
            jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDBLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldDB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDBLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextFieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextFielduser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabelURI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanelDBLayout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jButtonConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("DB connenction", jPanelDB);

        jLabel7.setText("Create new event");

        jLabel8.setText("Event id:");

        jLabelEventID.setText("  ");

        jLabel9.setText("Name:");

        jLabel10.setText("Track:");

        jTextField1.setText("lawn.png");

        jButtonCreate.setText("Create");
        jButtonCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCreateActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel11.setText("Use existing event");

        jButtonUse.setText("Use");
        jButtonUse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelEventLayout = new javax.swing.GroupLayout(jPanelEvent);
        jPanelEvent.setLayout(jPanelEventLayout);
        jPanelEventLayout.setHorizontalGroup(
            jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEventLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel7)
                    .addGroup(jPanelEventLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelEventID))
                    .addGroup(jPanelEventLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldEventName, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelEventLayout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonCreate)
                            .addComponent(jTextField1))))
                .addGap(27, 27, 27)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelEventLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jComboBoxExisting, 0, 157, Short.MAX_VALUE)))
                    .addGroup(jPanelEventLayout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jButtonUse)))
                .addContainerGap())
        );
        jPanelEventLayout.setVerticalGroup(
            jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEventLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelEventLayout.createSequentialGroup()
                            .addComponent(jLabel11)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBoxExisting, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonUse))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelEventLayout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(jLabelEventID, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(jTextFieldEventName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanelEventLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel10)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(jButtonCreate))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Event", jPanelEvent);

        jButtonStart.setText("Start");
        jButtonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jListMonitor);

        jButtonExit.setText("Exit");

        jButtonPort.setText("Change port");
        jButtonPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPortActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelMonitorLayout = new javax.swing.GroupLayout(jPanelMonitor);
        jPanelMonitor.setLayout(jPanelMonitorLayout);
        jPanelMonitorLayout.setHorizontalGroup(
            jPanelMonitorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMonitorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelMonitorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .addGroup(jPanelMonitorLayout.createSequentialGroup()
                        .addComponent(jButtonStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
                        .addComponent(jButtonPort)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExit)))
                .addContainerGap())
        );
        jPanelMonitorLayout.setVerticalGroup(
            jPanelMonitorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMonitorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelMonitorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonStart)
                    .addComponent(jButtonExit)
                    .addComponent(jButtonPort))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Ubisense", jPanelMonitor);

        jButtonStart1.setText("Start");
        jButtonStart1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStart1ActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jListMonitor1);

        jButtonExit1.setText("Exit");

        jButtonPort1.setText("Change port");
        jButtonPort1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPort1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelMonitor1Layout = new javax.swing.GroupLayout(jPanelMonitor1);
        jPanelMonitor1.setLayout(jPanelMonitor1Layout);
        jPanelMonitor1Layout.setHorizontalGroup(
            jPanelMonitor1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMonitor1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelMonitor1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .addGroup(jPanelMonitor1Layout.createSequentialGroup()
                        .addComponent(jButtonStart1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
                        .addComponent(jButtonPort1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExit1)))
                .addContainerGap())
        );
        jPanelMonitor1Layout.setVerticalGroup(
            jPanelMonitor1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMonitor1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelMonitor1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonStart1)
                    .addComponent(jButtonExit1)
                    .addComponent(jButtonPort1))
                .addContainerGap())
        );

        jTabbedPane1.addTab("GPS", jPanelMonitor1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
        logging();
    }//GEN-LAST:event_jButtonStartActionPerformed

    private void jTextFieldHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldHostActionPerformed
        uriUpdate();
    }//GEN-LAST:event_jTextFieldHostActionPerformed

    private void jTextFieldPortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPortActionPerformed
        uriUpdate();
    }//GEN-LAST:event_jTextFieldPortActionPerformed

    private void jTextFieldDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldDBActionPerformed
        uriUpdate();
    }//GEN-LAST:event_jTextFieldDBActionPerformed

    private void jTextFielduserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFielduserActionPerformed
        uriUpdate();
    }//GEN-LAST:event_jTextFielduserActionPerformed

    private void jPasswordFieldPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldPasswordActionPerformed
        uriUpdate();
    }//GEN-LAST:event_jPasswordFieldPasswordActionPerformed

    private void jButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConnectActionPerformed
        dbConnect();
    }//GEN-LAST:event_jButtonConnectActionPerformed

    private void jButtonUseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUseActionPerformed
        setEvent(false, String.valueOf(jComboBoxExisting.getSelectedItem()));
    }//GEN-LAST:event_jButtonUseActionPerformed

    private void jButtonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateActionPerformed
        setEvent(true, jTextFieldEventName.getText());
    }//GEN-LAST:event_jButtonCreateActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        startup();
    }//GEN-LAST:event_formWindowOpened

    private void jButtonPortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPortActionPerformed
        changePort();
    }//GEN-LAST:event_jButtonPortActionPerformed

    private void jButtonStart1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStart1ActionPerformed
    GPSLogging();
    }//GEN-LAST:event_jButtonStart1ActionPerformed

    private void jButtonPort1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPort1ActionPerformed
        changeGPSPort();
    }//GEN-LAST:event_jButtonPort1ActionPerformed

    /**
     * Show dialog and update port number
     */
    private void changePort() {

        String input = JOptionPane.showInputDialog(this, "Port number", "Enter new port number to use", JOptionPane.QUESTION_MESSAGE);
        if (input != null) {
            port = Integer.valueOf(input);
        }
    }

     /**
     * Show dialog and update port number
     */
    private void changeGPSPort() {

        String input = JOptionPane.showInputDialog(this, "Port number", "Enter new port number to use", JOptionPane.QUESTION_MESSAGE);
        if (input != null) {
            GPSPort = Integer.valueOf(input);
        }
    }

    private void startLogging() {
        //Disable option to change the port while running
        jButtonPort.setEnabled(false);
        //Switch button text
        jButtonStart.setText("Stop");
        //disable other tabs
        jPanelEvent.setEnabled(false);
        jPanelDB.setEnabled(false);

        //start ubisense connection
        logger = new DBLogger(port, dbc, table, this);
        logger.start();


        logging = true;
    }

    private void stopLogging() {
        logger.interrupt();
        //Enable option to change the port
        jButtonPort.setEnabled(true);
        //Switch button text
        jButtonStart.setText("Start");
        //eable other tabs
        jPanelEvent.setEnabled(true);
        jPanelDB.setEnabled(true);

        logging = false;

    }

    /**
     * Handle start/stop logging button press
     */
    private void logging() {

        if (!logging) {
            startLogging();
        } else {
            stopLogging();
        }
    }

    private void startGPSLogging() {
        //Disable option to change the port while running
        jButtonPort1.setEnabled(false);
        //Switch button text
        jButtonStart1.setText("Stop");
        //disable other tabs
        jPanelEvent.setEnabled(false);
        jPanelDB.setEnabled(false);

        //start ubisense connection
        GPSlogger = new DBLoggerGPS(GPSPort, dbc, table, this);
        GPSlogger.start();


        GPSLogging = true;
    }

    private void stopGPSLogging() {
        GPSlogger.interrupt();
        //Enable option to change the port
        jButtonPort1.setEnabled(true);
        //Switch button text
        jButtonStart1.setText("Start");
        //eable other tabs
        jPanelEvent.setEnabled(true);
        jPanelDB.setEnabled(true);

        GPSLogging = false;

    }

    /**
     * Handle start/stop logging button press
     */
    private void GPSLogging() {

        if (!GPSLogging) {
            startGPSLogging();
        } else {
            stopGPSLogging();
        }
    }



    /**
     * Sets up a new table to be used or selects an existing table
     * @param newEvent Does this need a new table in the DB
     * @param tableName Name for the new table, or table to be used
     */
    private void setEvent(Boolean newEvent, String tableName) {

        boolean setupErr = false;
        if (newEvent) {
            try {
                Statement st = dbc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //Set up DB query
                int rs = st.executeUpdate("create table " + tableName + " select * from eventtemplate");
                //rs.next();
                System.out.println(rs);
            } catch (SQLException ex) {
                setupErr = true;
                warning("Failed to create database", "Try a different name" + ex.getMessage());
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (!setupErr) {
            table = tableName;

            //Go to next tab, disable this one
            jTabbedPane1.setSelectedIndex(2);
            jPanelEvent.setEnabled(false);

        } else {
            jTextFieldEventName.setBackground(new Color(255, 200, 200));
        }

    }

    /**
     * Sets up the database connection
     */
    private void dbConnect() {
        uriUpdate();
        //Kill any existing connection
        if (dbc != null) {
            try {
                dbc.close();
            } catch (SQLException ex) {
                warning("Database error", "Failed to close existing connection" + ex.getMessage());
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        try {
            
            Class.forName("com.mysql.jdbc.Driver").newInstance();                       //Load DB driver

            dbc = DriverManager.getConnection(jLabelURI.getText(), "root", "test");     //Connect to DB

            //Query existing event data tables
            Statement st = dbc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //Set up DB query

            ResultSet rs = st.executeQuery("select dataset from eventindex");

            //Add names of existing events/tables to the list of existing events

            while (rs.next()) {
                jComboBoxExisting.addItem(rs.getString("dataset"));
            }
            if (jComboBoxExisting.getItemCount() < 1) {
                jComboBoxExisting.addItem("None found");
            }

            
            //Prepare GUI and move to next tab
            jLabelEventID.setText(String.valueOf(jComboBoxExisting.getItemCount() + 1));
            jPanelEvent.setEnabled(true);
            jTabbedPane1.setSelectedIndex(1);
            jButtonConnect.setText("Re-Connect");


            //Error handling
        } catch (InstantiationException ex) {
            warning("Database error", "Instatiation Exception" + ex.getMessage());
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            warning("Database error", "Illegal access exception" + ex.getMessage());
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            warning("Database error", "SQL error" + ex.getMessage());
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            warning("Database error", "Database driver not found" + ex.getMessage());
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Shortcut to pop a warning message
     * @param title Message title
     * @param message Message content
     */
    private void warning(String title, String message) {
        JOptionPane.showMessageDialog(this, title, message, JOptionPane.WARNING_MESSAGE);
    }

    /**
     * Generates the database URI when any text is entered
     */
    private void uriUpdate() {
        jLabelURI.setText("jdbc:mysql://" + jTextFieldHost.getText() + ":" + jTextFieldPort.getText() + "/" + jTextFieldDB.getText());
    }

    /**
     * Executed when GUI loads
     */
    private void startup() {

        //Set up monitor panel list
        monitorList = new DefaultListModel();
        jListMonitor.setModel(monitorList);

        //Add IP details to aid setting up Ubisense system
        try {
            monitorList.add(monitorList.getSize(), "Host details:");
            monitorList.add(monitorList.getSize(), "IP address: " + InetAddress.getLocalHost().getHostAddress());
            System.out.println("IP address: " + InetAddress.getLocalHost().getHostAddress());

        } catch (UnknownHostException ex) {
            monitorList.add(monitorList.getSize(), "Unable to get IP address");
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }

        monitorList.add(monitorList.getSize(), "Port number: " + String.valueOf(port));

    }

    public void addMessage(String message){
        monitorList.add(monitorList.getSize(), message);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
              gui =  new Admin();
              gui.setVisible(true);
                //JFrame.setDefaultLookAndFeelDecorated(false);



            }
        });
    }

    static Admin gui;
    private Connection dbc;     //Database connection
    private String table;       //Database table in use
    private DefaultListModel monitorList; //List for monitor panel
    private boolean logging = false; //Logging state
    private boolean GPSLogging = false; //GPSLogging state
    private int port = 1697; // Hard coded port number for Ubisense UDP sink
    private int GPSPort = 1698; // Hard coded port number for GPS UDP sink
    private DBLogger logger;
    private DBLoggerGPS GPSlogger;

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConnect;
    private javax.swing.JButton jButtonCreate;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonExit1;
    private javax.swing.JButton jButtonPort;
    private javax.swing.JButton jButtonPort1;
    private javax.swing.JButton jButtonStart;
    private javax.swing.JButton jButtonStart1;
    private javax.swing.JButton jButtonUse;
    private javax.swing.JComboBox jComboBoxExisting;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelEventID;
    private javax.swing.JLabel jLabelURI;
    private javax.swing.JList jListMonitor;
    private javax.swing.JList jListMonitor1;
    private javax.swing.JPanel jPanelDB;
    private javax.swing.JPanel jPanelEvent;
    private javax.swing.JPanel jPanelMonitor;
    private javax.swing.JPanel jPanelMonitor1;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextFieldDB;
    private javax.swing.JTextField jTextFieldEventName;
    private javax.swing.JTextField jTextFieldHost;
    private javax.swing.JTextField jTextFieldPort;
    private javax.swing.JTextField jTextFielduser;
    // End of variables declaration//GEN-END:variables
}
